<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/5/6
 * Time: 15:23
 */

namespace app\mobile\controller;


use app\Model\UsersModel;
use think\Controller;

class Login extends Controller
{
    /**
     *  登录页面
     */
    public function login()
    {


        if (session('user')){
            $this->redirect('user/index');
        }
        return view('login');
    }
    /**
     * 登录验证验证
     */
    public function do_login()
    {
        $username = input('post.username');
        $password = input('post.password');
        $username = trim($username);
        $password = trim($password);
        $logic = new UsersLogic();
        $res = $logic->login($username, $password);

        if ($res['status']==1){
            $this->success($res['msg'],null,$res);
//            $this->redirect('User/index');
        }
        $this->error($res['msg']);
    }
    /*
   * 用户注册页
   */
    public function reg()
    {

        return view();
    }
    public function logout()
    {
        session('user',null);
        setcookie('cn', '', time() - 3600, '/');
        $this->redirect('Login/login');

    }
    /*
     *
     * 注册信息验证
     */
    public function regver()
    {
        $getData= input('post.');
        $where=[
            'mobile'=>$getData['mobile'],
            'is_del'=>0
        ];
        $getUser=UsersModel::where($where)->find();//手机是否存在
        if ($getUser){return retJson('','201','该手机已经注册');}
        //验证码
        $code=session($getData['mobile'])['code'];
//        dump((string)$code);
//        dump($getData['mobile_code']);


        if ((string)$code!=$getData['mobile_code']){
            return retJson('','201','验证码错误');
        }

        $insert = [
            'mobile'=>$getData['mobile'],
            'password'=>encryption($getData['password']),
            'nickname'=>$getData['mobile'],//昵称（先使用手机号）
            'reg_time'=>time(),
        ];
        //查找推荐人id
        $get_user1= UsersModel::where('mobile',$getData['up_mobile'])->where('is_del',0)->find();
        if ($get_user1){
            $up_user1=$get_user1->user_id;//第一上级id
            $insert['first_leader']=$up_user1;
            $up_user2=$get_user1->first_leader;//第二上级id
            $insert['second_leader']=$up_user2;
        }
        $addUser=UsersModel::insertGetId($insert);
        $getUser=UsersModel::find($addUser);
        session('user',$getUser);
        return setDatabase($addUser,'用户注册');
    }
    /**
     * 忘记密码 页面跳转
     */
    public function forget_pwd(){
        return view('find_pwd');
    }
    /**
     * 修改密码(忘记密码)
     */
    public function changepwd(){

        if (input('mobile') == ''){
            return retJson('', 201, '请输入手机号！');
        }
        if (strlen(input('mobile')) != 11 ){
            return retJson('', 201, '请输入正确的手机号！');
        }
        if (input('mobile_code') == ''){
            return retJson('', 201, '请输入验证码！');
        }
        if (input('password') == ''){
            return retJson('', 201, '请输入新密码！');
        }
        if (input('newpass') == ''){
            return retJson('', 201, '请确认密码！');
        }
        if (input('password') != input('newpass')){
            return retJson('', 201, '两次密码不相同！');
        }
        if (session(input('mobile')) == ''){
            return retJson('', 201, '该号码未获取验证码,请检查号码是否正确或重新获取验证码！');
        };

        if (input('mobile_code') != session(input('mobile'))['code']){
            return retJson('', 201, '验证码错误！');
        }
        $res = UsersModel::update([
            'password'=>encryption(input('password'))],
            ['mobile'=>input('mobile')]);

        if(!$res){
            return retJson('', 201, '修改失败！');
        }
        session(input('mobile'),null);

        return retJson('', 200, '修改成功！');
    }
    /**
     *短信验证 忘记密码
     */
    public function smsver(){
        if (cookie(input('mobile')) == 1){
            return retJson('', 201, '距离上次验证码发送不超过一分钟！');
        }
        $code = rand(1000,9999);
        $mobile = input('mobile');

        $res = UsersModel::field('*')->where('mobile',$mobile)->where('is_del',0)->find();
        if (!$res){
            return retJson('', 201, '手机号未注册！');
        }
        session($mobile,array('mobile'=>$mobile,'code'=>$code));
        $send = sendMsg($mobile,$code);

        if ($send['Message'] != 'OK'){
            return retJson('', 201, '短信发送失败！');
        }
        cookie($mobile,1,60);
        return retJson('', 200, '短信已发送！');
    }

    /*
  *短信 注册用户
  */
    public function vercod()
    {
//        cookie('time',null);die();

        if (cookie('time') == 1){
            return retJson('', 201, '距离上次验证码发送不超过一分钟！');
        }
        $code = rand(1000,9999);
        $mobile = input('mobile');

        $res = UsersModel::field('*')->where('mobile',$mobile)->where('is_del',0)->find();
        if ($res){
            return retJson('', 201, '手机号已注册！');
        }
        session($mobile,array('mobile'=>$mobile,'code'=>$code));

        $send = sendMsg($mobile,$code);
//        return retJson('', 201, $send);

        if ($send['Message'] != 'OK'){
            return retJson('', 201, '短信发送失败！');
        }
        cookie('time',1,60);
//        return retJson('', 200, session($mobile));
        return retJson('', 200, '短信已发送！');
    }
}